﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;

namespace Thor.Common
{
    public class Time
    {
        [Key]
        public int Value { get; set; }
        public string Text { get; set; }

        public override string ToString()
        {
            return string.Format("{0}|{1}", Value.ToString(), Text);
        }

        public static Time GetTime(int value)
        {
            return (from time in GetTimes()
                    where time.Value == value
                    select time).FirstOrDefault();
        }

        public static Time GetTime(string text)
        {
            return (from time in GetTimes()
                    where time.Text == text
                    select time).FirstOrDefault();
        }

        public static Time GetTime(DateTime dateTime)
        {
            return (from time in GetTimes()
                    where time.Text.ToLower() == dateTime.ToShortTimeString().ToLower()
                    select time).FirstOrDefault();
        }

        public static List<Time> GetTimeframe(int start, int end)
        {
            return (from time in GetTimes()
                    where time.Value >= start && time.Value <= end
                    select time).ToList();
        }

        public static List<Time> GetTimes()
        {
            List<Time> times = new List<Time>();

            int decrement = 0;
            int baseHour = 12;
            int hour = 12;
            string minute = "00";
            string meridian = "AM";

            for (int i = 1; i <= 48; i++)
            {
                if (i == 1 || i == 25)
                    hour = 12;

                if (i == 2 || i == 26)
                    decrement = 11;

                var remainder = i % 2;

                if (remainder == 0)
                    minute = "30";
                else
                    minute = "00";

                if (i == 48)
                    minute = "59";

                if (i >= 25)
                    meridian = "PM";

                string text = string.Format("{0}:{1} {2}", hour.ToString(), minute, meridian);
                Time time = new Time() { Value = i, Text = text };

                times.Add(time);

                if (remainder == 0)
                {
                    hour = baseHour - decrement;
                    decrement -= 1;
                }

            }

            return times;
        }
    }
}
